// ml278
// Copyright (C) 2008 Bjørn Øivind Bjørnsen, Aleksander Gjermundsen, Knut Andre Tidemann, Thomas Hansen
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#ifndef __PLANETARYINDEXBUFFERMANAGER_H_
#define __PLANETARYINDEXBUFFERMANAGER_H_

#include <Ogre.h>
#include <vector>
#include <utility>

#define INDEX_BUFFER_TILE 0
#define INDEX_BUFFER_PAGE 1
#define INDEX_BUFFER_ZONE 2
#define INDEX_BUFFER_ATMO 3
#define INDEX_BUFFER_ZONE_TO_PAGE_JOINT 4
#define INDEX_BUFFER_PAGE_TO_TILE_JOINT 5

namespace Ogre
{
	class PlanetarySceneManager;
	class PlanetaryOptions;

	/**
	* Creates and manages index buffers.
	* Since one type of renderable object, i.e. pages, zones, tiles uses the same layout of the triangles, we only need to create index buffers for
	* each type once. This class does that and creates if it's needed.
	*/
	class PlanetaryIndexBufferManager
	{
	private:
		/// Internal struct used to keep track of already created index buffers
		struct INDEXBUFFERINFO
		{
			/// The type of index buffer this is
			unsigned int type;
			/// Lod level of this index buffer
			unsigned int LOD;
			/// Pointer to the actual buffer
			HardwareIndexBufferSharedPtr buffer;

			/// Pointer to a shadow buffer. Read only
			int *MemoryData;
		};

	public:
		PlanetaryIndexBufferManager(PlanetarySceneManager *mgr);
		~PlanetaryIndexBufferManager();

		/// Get index buffer for the requested type, only one implemented for now
		HardwareIndexBufferSharedPtr getIndexBuffer(unsigned int type, unsigned int LOD = 0);
		int *getIndexShadowBuffer(unsigned int type, unsigned int LOD = 0);

		/// Get the size of the requested index buffer
		unsigned int getIndexBufferSize(unsigned int type, unsigned int LOD = 0);

		/// Clears all loaded index buffers
		void clear();
	private:
		/// Create a index buffer for a tile.
		/// @param LOD LOD level to create index buffer for
		INDEXBUFFERINFO *createTileIndexBuffer(unsigned int LOD);
		/// Create a index buffer for a page.
		/// @param LOD LOD level to create index buffer for
		INDEXBUFFERINFO *createPageIndexBuffer(unsigned int LOD);
		/// Create a index buffer for a zone.
		/// @param LOD LOD level to create index buffer for
		INDEXBUFFERINFO *createZoneIndexBuffer(unsigned int LOD);
		/// Create a index buffer for the atmosphere.
		/// @param LOD LOD level to create index buffer for
		INDEXBUFFERINFO *createAtmoIndexBuffer(unsigned int LOD);
		/// Create a index buffer for a Zone to Page joint.
		INDEXBUFFERINFO *createZoneToPageJoint();
		/// Create a index buffer for a Page to Tile joint.
		INDEXBUFFERINFO *createPageToTileJoint();

		/// Pointer to scenemanager
		PlanetarySceneManager *mSceneMgr;

		/// Pointer to options object
		PlanetaryOptions *mOptions;
		
		/// Container for all loaded index buffers
		std::vector<INDEXBUFFERINFO *> mIndices;

	};
}
#endif
